#!/usr/bin/env python
# -*- coding: utf-8 -*-

# install pymongo : $pip install pymongo, $easy_install pymongo

from pymongo import Connection

class MGDB(object):
    def __init__(self,dbname=None,  cname=None, ip='localhost', port=27017):
        self.conn = Connection(ip, port) 
	self.set_collection(dbname, cname)

    def set_collection(self, dbname=None, cname=None):
        self.db = self.conn[dbname] if (dbname and self.conn) else None
        self.coll = self.db[cname] if (cname and self.db) else None  

    def insert(self, e):
        return self.coll.insert(e) if self.coll else False
        
    def list(self, c={}):
        return self.coll.find(c) if self.coll else []

    def update(self, c, u):
        return self.coll.update(c, {'$sef':u}) if self.coll else False

    def remove(self, c):
        return self.coll.remove(c) if self.coll else False

if __name__ == '__main__':
    m = MGDB('testdb', 'post')
    d = {'name':'서울시', 'tel':'4121070'}
    m.insert(d)
    m.list(d)
    m.remove(d)    
